<?php

	class Model_Participant extends Zend_Db_Table_Abstract
	{
   	protected $_table;

    public function getTable()
    {
        if (null === $this->_table) {            
            require_once APPLICATION_PATH . '/models/DbTable/Participant.php';
            $this->_table = new Model_DbTable_Participant;
        }
        return $this->_table;
    }
   
    public function save(array $data)
    {
    	if(isset($data['id'])){
    		$id = $data['id'];
    	}else{
    		$id = 0;
    	}
    	
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);        
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);                
            }
            
        }
        
        if($id == 0){
        	return $table->insert($data);
        }else{
        	$where = $table->getAdapter()->quoteInto('id = ?', $id);
        	return $table->update($data, $where);
        }
    }
    public function checkEmail($email){
    	$table = $this->getTable();
      	$select = $table->select()->where('email = ?', $email);
      	$row = $table->fetchRow($select);
      	return isset($row);
    }
    public function getByEmail($email){
        $table = $this->getTable();
      	$select = $table->select()->where('email = ?', $email);
      	$row = $table->fetchRow($select);
        return $row;
    }
	public function checkPass($pass, $id){
    	$table = $this->getTable();
      	$select = $table->select()->where('id = ?', $id)->where('password = ?', md5($pass));
      	$row = $table->fetchRow($select);
      	return isset($row);
    }
    public function fetchEntries()
    {       
        return $this->getTable()->fetchAll('1')->toArray();
    }
	    
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('id = ?', $id);    
        return $table->fetchRow($select);
    }
    public function getParticipants($sort, $dir, $start, $limit){
    	$table = $this->getTable();
      	$select = $table->select();
        $select->order($sort . ' ' . $dir);
        $select->limit($limit, $start);
		return $table->fetchAll($select)->toArray();
    }
    public function getParticipantsFilter($sort, $dir, $start, $limit, $filter){
    	$table = $this->getTable();
      	$select = $table->select();
        if(isset($filter)){
            foreach($filter as $f){
                $q = $f['field'] . ' LIKE ?'; 
                $v = '%' . $f['data']['value'] . '%';                
                $select->where($q, $v);
            }
        }
		$select->order($sort . ' ' . $dir)->limit($limit, $start);
		return $table->fetchAll($select)->toArray();
    }
    public function getTotalFilter($filter){
    	$table = $this->getTable();		 					        
        $select = $table->select();
        $select->from($table,array('count' => 'COUNT(id)'));
        if(isset($filter)){
            foreach($filter as $f){
                $q = $f['field'] . ' LIKE ?'; 
                $v = '%' . $f['data']['value'] . '%';                
                $select->where($q, $v);
            }
        }      
		$res = $table->fetchRow($select);        
        $c = $res->toArray();
    	return $c['count'];
    }
    public function setReviewers($list){
		$count = count($list);
		for($i = 0; $i < $count; $i++){
			$p = $this->fetchEntry($list[$i]);                        
            if($p->isReviewer){
                $p->isReviewer = 0;            
            }else{
                $p->isReviewer = 1;            
                $p->isReviewer = 1;            
            }
			$p->save();
		}
    }
    public function setActivated($list){
		$count = count($list);
		for($i = 0; $i < $count; $i++){
			$p = $this->fetchEntry($list[$i]);                        
            $p->stamp = 1;                        
			$p->save();
		}
    }
    public function getReviewers($sort, $dir, $limit, $start)
    {
        $table = $this->getTable();
		$db = $table->getAdapter();	
		$sql = 'SELECT (SELECT COUNT(a.id) from assignments a WHERE a.reviewer_id = p.id AND a.isActive = 1) as total_papers, 
				p.id, p.first_name, p.last_name, p.institution, p.email
				FROM participants p
				WHERE p.isReviewer = 1
				ORDER BY ' . $sort . ' ' . $dir .' '.'
				LIMIT ' . $start . ', ' . $limit;

		$res = $db->query($sql);
        return $res->fetchAll();
    } 	
	public function getReviewersCount(){
    	$table = $this->getTable();		 			
		$db = $table->getAdapter();	
		$sql = 'SELECT COUNT(id) FROM participants WHERE isReviewer = 1';
		$res = $db->query($sql);    	
    	return $res->fetchColumn(0);
    }
    public function authenticate($email, $password){
      $table = $this->getTable();
      $select = $table->select()
                    ->where('email = ?', $email)
                    ->where('password = ?', md5($password));      
      return $table->fetchRow($select)->toArray();
    }
    
    public function confirmRegistration($id, $stamp){
    	$table = $this->getTable();
        $select = $table->select()->where('id = ?', $id);                    
        $row = $table->fetchRow($select);        
        if(isset($row)){
            if($row->stamp == $stamp){        
      	        $where = $table->getAdapter()->quoteInto('id = ?', $id);
      	        $data = array();
      	        $data['stamp'] = 1;
                $table->update($data, $where);
                return true;
            }
            if($row->stamp == 1){
                return true;
            }                
        }
        return false;
    }
    
    public function deleteParticipant($id){
    	$table = $this->getTable();
    	$where = 'id = '.$id;
    	return $table->delete($where); 
    }
}